package net.blank.keyboardUtl
{
	import flash.display.DisplayObjectContainer;
	import flash.events.KeyboardEvent;
	import flash.utils.Dictionary;

	/**
	 * 管理
	 * @author blank
	 * 
	 */
	public class KeyboardManger
	{
		/**
		 * 被按钮下的键的集合
		 */
		private static var downKeys:Dictionary;
		
		/**
		 * 判断某键是否被按下
		 * @param keyCode 键值
		 * @return 
		 * 
		 */
		public static function isDownForKey(keyCode:uint):Boolean{
			if(keyCode in downKeys){
				return downKeys[keyCode];
			}
			return false;
		}
		
		/**
		 * 按键被按下
		 * @param evt
		 * 
		 */
		private static function keyDownHanlder(evt:KeyboardEvent):void{
			downKeys[evt.keyCode]=true;
		}
		
		/**
		 * 按键被释放
		 * @param evt
		 * 
		 */
		private static function keyUpHandler(evt:KeyboardEvent):void{
			downKeys[evt.keyCode]=false;
		}
		
		/**
		 * 初始化
		 * @param container 需要存储被按下键的容器
		 * 
		 */
		public static function init(container:DisplayObjectContainer):void{
			downKeys=new Dictionary();
			
			container.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHanlder);
			container.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
		}
	}
}